﻿using SitefinityWatch.Web.UI.Editors;
using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Telerik.Framework.Web;

namespace SitefinityWatch.Web.UI.Design
{
    public class SubLinkDesigner : Telerik.Framework.Web.Design.ControlDesigner
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            sublinks = (SubLinks)DesignedControl;
        }

        protected override void CreateChildControls()
        {
            HtmlLink link = new HtmlLink();
            link.Href = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "SitefinityWatch.Web.UI.Resources.designer.css");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            this.Page.Header.Controls.Add(link);

            container = new SubLinkDesignerContainer();

            container.StartNodeMode.Items.FindByValue(sublinks.StartNodeMode.ToString().Trim()).Selected = true;
            container.Columns.Items.FindByValue(sublinks.Columns.ToString().Trim()).Selected = true;

            container.StartNodeMode.SelectedIndexChanged += new EventHandler(StartNodeMode_SelectedIndexChanged);
            container.StartNodeMode.AutoPostBack = true;

            container.SiteMapTree.Value = sublinks.PageId;

            SetSiteMapTreeVisibility();

            this.Controls.Add(container);
        }

        public override void OnSaving()
        {
            sublinks.Columns = Convert.ToInt32(container.Columns.SelectedValue);

            if (container.StartNodeMode.SelectedValue == SubLinks.StartNodeModes.CurrentPage.ToString())
            {
                sublinks.StartNodeMode = SubLinks.StartNodeModes.CurrentPage;
            }
            else
            {
                sublinks.StartNodeMode = SubLinks.StartNodeModes.SelectedPageId;
                sublinks.PageId = container.SiteMapTree.Value;
            }
        }

        void StartNodeMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetSiteMapTreeVisibility();
        }

        void SetSiteMapTreeVisibility()
        {
            if (container.StartNodeMode.SelectedValue == SubLinks.StartNodeModes.CurrentPage.ToString())
            {
                container.PageIdSelectorPanel.Visible = false;
            }
            else
            {
                container.PageIdSelectorPanel.Visible = true;
            }
        }

        private SubLinkDesignerContainer container;
        private SubLinks sublinks;

        public class SubLinkDesignerContainer : Telerik.Cms.Web.UI.GenericContainer
        {
            public SubLinkDesignerContainer()
            {
                Controls.Add(ControlUtils.GetTemplateFromResource("SitefinityWatch.Web.UI.Design.SubLinkDesigner.ascx", this.GetType()));
            }

            public Panel PageIdSelectorPanel
            {
                get
                {
                    return base.GetControl<Panel>("PageIdSelectorPanel", true);
                }
            }

            public PageIdSelector SiteMapTree
            {
                get
                {
                    return base.GetControl<PageIdSelector>("SiteMapTree", true);
                }
            }

            public DropDownList Columns
            {
                get
                {
                    return base.GetControl<DropDownList>("Columns", true);
                }
            }

            public DropDownList StartNodeMode
            {
                get
                {
                    return base.GetControl<DropDownList>("StartNodeMode", true);
                }
            }
        }
    }
}
